home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / util / cli / MCommands_1_2.lha / Src / changefont.c next >
C/C++ Source or Header  |  1992-09-02  |  6KB  |  191 lines

  1. /****************************************************************************/
  2. /*                               changefont.c                               */
  3. /*                       Change default system font.                        */
  4. /*                    Copyright © 1994 Michael Letowski                     */
  5. /****************************************************************************/
  6.  
  7. #define __USE_SYSBASE
  8.  
  9. #include <exec/types.h>
  10. #include <exec/execbase.h>
  11. #include <exec/memory.h>
  12. #include <dos/rdargs.h>
  13. #include <dos/dosextens.h>
  14. #include <graphics/text.h>
  15. #include <support/types.h>
  16. #include <support/exec.h>
  17. #include <support/dos.h>
  18.  
  19. #include <string.h>
  20.  
  21. #include <proto/exec.h>
  22. #include <proto/dos.h>
  23. #include <proto/diskfont.h>
  24. #include <proto/graphics.h>
  25.  
  26. #include "changefont.rev.h"
  27.  
  28. #define DOS_NAME            "dos.library"
  29. #define DOS_VERN             37L
  30. #define GFX_NAME            "graphics.library"
  31. #define GFX_VERN            0L
  32. #define DISKFONT_NAME    "diskfont.library"
  33. #define DISKFONT_VERN    36L
  34.  
  35. #define STD_NAME            "topaz.font"
  36. #define STD_STYLE            FS_NORMAL
  37. #define STD_FLAGS            (FPF_ROMFONT | FPF_DESIGNED)
  38.  
  39. #define REPLACE_FLAGS    (FPF_DISKFONT | FPF_DESIGNED)
  40.  
  41. #define FLAGS_MASK        (FPF_REVPATH | FPF_TALLDOT | FPF_WIDEDOT | FPF_PROPORTIONAL | FPF_DESIGNED)
  42.  
  43. #define TEMPLATE            "NAME/A,SIZE/N/M,QUIET/S"
  44.  
  45. STATIC CONST TEXT VersionString[]=
  46.     VERSION(PROG_NAME,PROG_VERSION,PROG_REVISION,PROG_DATE);
  47.  
  48. #define MSG_REPLACED            "  Replaced\n"
  49. #define MSG_NOT_REPLACED    "  Not Replaced"
  50. #define INFO_TEMPLATE            "%s %d"
  51.  
  52. #define FONT_EXT            ".font"
  53.  
  54. #define OPT_NAME        0
  55. #define OPT_SIZE        1
  56. #define OPT_QUIET        2
  57. #define OPT_COUNT        3
  58.  
  59. #define NAME_SIZE        32        /* Size of name buffer */
  60. #define BASE_SIZE        25        /* Max allowed length of font's name w/o extension */
  61. #define EXT_SIZE        5            /* Size of font extension */
  62. #define FONT_SIZE        (BASE_SIZE+EXT_SIZE)
  63.  
  64. STATIC BOOL Match(struct TextFont *srcFont, struct TextFont *destFont);
  65. STATIC VOID CopyAttrs(struct TextFont *srcFont, struct TextFont *destFont);
  66.  
  67. ULONG ChangeFont(VOID)
  68. {
  69.     struct ExecBase *SysBase=*((struct ExecBase **)4);
  70.     struct DosLibrary *DOSBase;
  71.     struct GfxBase *GfxBase;
  72.     struct Library *DiskfontBase;
  73.  
  74.     STATIC CONST LONG Default8=8,Default9=9;
  75.     STATIC CONST LONG * CONST DefaultSizes[]={&Default8,&Default9,NULL};
  76.  
  77.     LONG Opts[OPT_COUNT];
  78.     ULONG RC=RETURN_FAIL,FontError;
  79.     struct TextAttr StdAttr,ReplaceAttr;
  80.     struct TextFont *StdFont,*ReplaceFont;
  81.     CHAR FontName[NAME_SIZE];
  82.     LONG **SizesPtr,*TempPtr;
  83.     struct RDArgs *Args;
  84.     BOOL ErrOccured=FALSE;
  85.  
  86.     unless(DOSBase=(struct DosLibrary *)OpenLibrary(DOS_NAME,DOS_VERN))
  87.     {
  88.         SetResult2(ERROR_INVALID_RESIDENT_LIBRARY);
  89.         goto InvalidDOS;
  90.     }
  91.  
  92.     unless(GfxBase=(struct GfxBase *)OpenLibrary(GFX_NAME,GFX_VERN))
  93.     {
  94.         CauseIoErr(ERROR_INVALID_RESIDENT_LIBRARY,NULL);
  95.         goto InvalidGfx;
  96.     }
  97.  
  98.     unless(DiskfontBase=OpenLibrary(DISKFONT_NAME,DISKFONT_VERN))
  99.     {
  100.         CauseIoErr(ERROR_INVALID_RESIDENT_LIBRARY,NULL);
  101.         goto InvalidDiskfont;
  102.     }
  103.  
  104.     clear(&Opts);                                                            /* Clear out args buffer */
  105.     unless(Args=ReadArgs(TEMPLATE,Opts,NULL))
  106.     {
  107.         PrintFault(IoErr(),NULL);                                /* Inform user */
  108.         goto NoArgs;
  109.     }
  110.  
  111.     StdAttr.ta_Name=STD_NAME;
  112.     StdAttr.ta_Flags=STD_FLAGS;
  113.  
  114.     ReplaceAttr.ta_Name=FontName;
  115.     ReplaceAttr.ta_Flags=REPLACE_FLAGS;
  116.  
  117.     ReplaceAttr.ta_Style=StdAttr.ta_Style=STD_STYLE;
  118.  
  119.     SizesPtr=Opts[OPT_SIZE] ? (LONG **)Opts[OPT_SIZE] : DefaultSizes;
  120.     while(TempPtr=*SizesPtr++)
  121.     {
  122.         FontError=ERROR_OBJECT_NOT_FOUND;    /* Possible error: font not found */
  123.         ReplaceAttr.ta_YSize=StdAttr.ta_YSize=*TempPtr;
  124.         unless(StdFont=OpenDiskFont(&StdAttr))
  125.             goto NoStdFont;
  126.  
  127.         clear(&FontName);
  128.         strncpy(FontName,(STRPTR)Opts[OPT_NAME],FONT_SIZE);
  129.         unless(ReplaceFont=OpenDiskFont(&ReplaceAttr))
  130.         {
  131.             if(strlen(FontName)>BASE_SIZE)    /* Name too long */
  132.                 goto NoReplaceFont;
  133.             strcat(FontName,FONT_EXT);            /* Now try concatenating ".font" */
  134.             unless(ReplaceFont=OpenDiskFont(&ReplaceAttr))
  135.                 goto NoReplaceFont;
  136.         }
  137.  
  138.         FontError=ERROR_OBJECT_WRONG_TYPE;    /* Possible error: wrong type of font */    
  139.         if(Match(ReplaceFont,StdFont))            /* Fonts match themselves */
  140.         {
  141.             Forbid();
  142.             CopyAttrs(ReplaceFont,StdFont);
  143.             Permit();
  144.             unless(Opts[OPT_QUIET])
  145.                 VPrintf(INFO_TEMPLATE MSG_REPLACED,&StdAttr);
  146.             continue;
  147.         }
  148.         CloseFont(ReplaceFont);
  149. NoReplaceFont:
  150.         CloseFont(StdFont);
  151. NoStdFont:
  152.         VPrintf(INFO_TEMPLATE,&StdAttr);        /* Trick: fields in TextAttr */
  153.                                                                                 /* Structure match template parameters */
  154.         CauseIoErr(FontError,MSG_NOT_REPLACED);
  155.         ErrOccured=TRUE;
  156.     }
  157.     RC=ErrOccured ? RETURN_ERROR : RETURN_OK;
  158.  
  159.     FreeArgs(Args);
  160. NoArgs:
  161.     CloseLibrary(DiskfontBase);
  162. InvalidDiskfont:
  163.     CloseLibrary((struct Library *)GfxBase);
  164. InvalidGfx:
  165.     CloseLibrary((struct Library *)DOSBase);
  166. InvalidDOS:
  167.     return(RC);
  168. }
  169.  
  170. STATIC BOOL Match(struct TextFont *srcFont, struct TextFont *destFont)
  171. {
  172.     return((BOOL)(srcFont->tf_YSize==destFont->tf_YSize &&
  173.                                 srcFont->tf_Style==destFont->tf_Style &&
  174.                                 (srcFont->tf_Flags & FLAGS_MASK)==
  175.                                     (destFont->tf_Flags & FLAGS_MASK) &&
  176.                                 srcFont->tf_XSize==destFont->tf_XSize &&
  177.                                 srcFont->tf_Baseline==destFont->tf_Baseline));
  178. }
  179.  
  180. STATIC VOID CopyAttrs(struct TextFont *srcFont, struct TextFont *destFont)
  181. {
  182.     destFont->tf_BoldSmear=srcFont->tf_BoldSmear;
  183.     destFont->tf_Modulo=srcFont->tf_Modulo;
  184.     destFont->tf_LoChar=srcFont->tf_LoChar;
  185.     destFont->tf_HiChar=srcFont->tf_HiChar;
  186.     destFont->tf_CharData=srcFont->tf_CharData;
  187.     destFont->tf_CharLoc=srcFont->tf_CharLoc;
  188.     destFont->tf_CharSpace=srcFont->tf_CharSpace;
  189.     destFont->tf_CharKern=srcFont->tf_CharKern;
  190. }
  191.